from core.basehandler import BaseHandler
from core.room import Room
from core.action import Action
from core.support import get_game
from items.translator import Translator

class CPU(Room):
    def __init__(self, room_id):
        Room.__init__(self, room_id)    # do basic initialization for every room
                          
        # build list of actions
        self.add_action(North)
        self.add_action(South)
        self.add_action(East)
        self.add_action(West)
    
    # return description of room.
    def get_description(self):
        if self.is_ascii():
            return '''
            It looks like the ASCII was the last piece needed to complete a binary translator. Might just
            help you understand this world better...
            '''
        else:
            return '''
            Whoa....You look around and realize that you are now inside your computer!  
            On further inspection, you notice that you are currently located in the CPU area with four ways to go.
            '''
    
    # return image of room
    def get_image(self):
        if self.has_translator():
            return 'cpu.png'
        else:
            return 'cpubin.png'
        #return 'binary_monster.jpg'
    
    def get_image_width(self):
        return 'width=100%'

    def is_ascii(self):
        
        game = get_game()
            
        # does the player have ASCII from the register?
        player = game.get_player()
        
        if "Ascii" in player.inventory:
            return True
        # if we get here, player does not have ASCII
        return False
    
    def has_translator(self):
        
        game = get_game()
            
        # does the player have ASCII from the register?
        player = game.get_player()
        
        if "Translator" in player.inventory:
            return True
        # if we get here, player does not have ASCII
        return False

class CPUHandler(BaseHandler):
    
    def get_template(self):
        return 'cpu.html'
    
    # return class to use to create a new instance of this room
    def get_room_class(self):
        return CPU
    
    # handles both get and post requests
    def update(self,request_type):
        if self.get_this_room().is_ascii() and not self.get_this_room().has_translator():
            self.get_this_room().add_item(Translator())
        BaseHandler.update(self, request_type);
        

# define actions for this room

class North(Action):
    def __init__(self):
        Action.__init__(self, "North")    # do basic initialization for every action
    
    # return description of action (used in label on webpage)
    def get_description(self):
        game = get_game()
        room = game.get_room("cpu", CPU)
        if room.has_translator():
            return "Go North"
        else:
            return "11"
    
    # return id of room to enter when action is complete
    def get_destination(self):
        return 'register'
    
    # return http method to use when user clicks on this action
    # use "get" if just moving to another room.  if changing something
    # like the state of an inventory item or room then use "post"
    def get_method(self):
        return "get"

class South(Action):
    def __init__(self):
        Action.__init__(self, "South")    # do basic initialization for every action
    
    # return description of action (used in label on webpage)
    def get_description(self):
        game = get_game()
        room = game.get_room("cpu", CPU)
        if room.has_translator():
            return "Go South"
        else:
            return "00"
    
    # return id of room to enter when action is complete
    def get_destination(self):
        return 'fail'
    
    # return http method to use when user clicks on this action
    # use "get" if just moving to another room.  if changing something
    # like the state of an inventory item or room then use "post"
    def get_method(self):
        return "get"
    
class East(Action):
    def __init__(self):
        Action.__init__(self, "East")    # do basic initialization for every action
    
    # return description of action (used in label on webpage)
    def get_description(self):
        game = get_game()
        room = game.get_room("cpu", CPU)
        if room.has_translator():
            return "Go East"
        else:
            return "01"
    
    # return id of room to enter when action is complete
    def get_destination(self):
        return 'Binary_Monster'
    
    # return http method to use when user clicks on this action
    # use "get" if just moving to another room.  if changing something
    # like the state of an inventory item or room then use "post"
    def get_method(self):
        return "get"

class West(Action):
    def __init__(self):
        Action.__init__(self, "West")    # do basic initialization for every action
    
    # return description of action (used in label on webpage)
    def get_description(self):
        game = get_game()
        room = game.get_room("cpu", CPU)
        if room.has_translator():
            return "Go West"
        else:
            return "10"
    
    # return id of room to enter when action is complete
    def get_destination(self):
        return 'busnodriver'
    
    # return http method to use when user clicks on this action
    # use "get" if just moving to another room.  if changing something
    # like the state of an inventory item or room then use "post"
    def get_method(self):
        return "get"
